package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.BusinessLayer;
import cn.fintecher.gis.entity.DistrictInfo;
import cn.fintecher.gis.entity.SupplyFile;
import cn.fintecher.gis.entity.SupplyInfo;
import cn.fintecher.gis.enums.CommonShow;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.MessageType;
import cn.fintecher.gis.model.request.FlowApplyRequest;
import cn.fintecher.gis.model.request.LayerInfoCreateRequest;
import cn.fintecher.gis.model.request.SupplyInfoRequest;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.utils.BeanUtils;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 18:18 2019/4/29
 */

@Service("supplyInfoService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class SupplyInfoService extends BaseService {
    /**
     * 新增供地信息
     */
    public void addSupplyInfo(SupplyInfoRequest request, SessionModel model) throws Exception {
        SupplyInfo supplyInfo = new SupplyInfo();
        BeanUtils.copyPropertiesIgnoreNull(request, supplyInfo);
        LocalDate dateTime = LocalDate.parse(request.getSupplyTime(), DateTimeFormat.forPattern("yyyy-MM-dd"));
        supplyInfo.setSupplyTime(dateTime.toDate());
        if (Objects.nonNull(request.getLayerBaseRequest())) {
            LayerInfoCreateRequest layerInfoCreateRequest = new LayerInfoCreateRequest();
            BeanUtils.copyPropertiesIgnoreNull(request.getLayerBaseRequest(), layerInfoCreateRequest);
            List<String> styleNames = new ArrayList<>();
            styleNames.add(request.getLayerBaseRequest().getStyleName());
            layerInfoCreateRequest.setStyleNames(styleNames);
            BusinessLayer businessLayer = getLayerInfo(layerInfoCreateRequest, FlowType.SUPPLY, model);
            supplyInfo.setBusinessLayer(businessLayer);
        }
        Set<DistrictInfo> infoSet = getDistrictInfoByIds(request.getRegions());
        supplyInfo.setRegions(infoSet);
        grantInfoRepository.findById(request.getGrantId()).ifPresent(grantInfo -> {
            supplyInfo.setGrantInfo(grantInfo);
            return;
        });
        supplyInfoRepository.save(supplyInfo);
        if (Objects.nonNull(request.getFileIds()) && !request.getFileIds().isEmpty()) {
            List<SupplyFile> list = new ArrayList<>();
            for (String fileId : request.getFileIds()) {
                SupplyFile supplyFile = new SupplyFile();
                supplyFile.setSupplyId(supplyInfo.getId());
                supplyFile.setFileId(fileId);
                list.add(supplyFile);
            }
            supplyFileRepository.saveAll(list);
        }

        //添加流程申请
        FlowApplyRequest flowApplyRequest = new FlowApplyRequest();
        flowApplyRequest.setApplyId(supplyInfo.getId());
        flowApplyRequest.setApplyUserId(model.getId());
        flowApplyRequest.setType(FlowType.SUPPLY);
        flowApplyRequest.setNextUserId(request.getNextUserId());
        if (request.getWhether().equals(CommonShow.YES)) {
            flowApplyRequest.setNextUserId(request.getNextUserId());
        }
        flowApplyRequest.setWhether(supplyInfo.getWhether());
        flowService.flowApply(flowApplyRequest, MessageType.SUPPLY);
    }
}
